<?php 
/**
*	wps对接在线文档接口回调的
*/
class v1ClassAction extends apiAction
{
	

	public function int3rdAction()
	{
		$this->apipath 	= arrvalue($GLOBALS,'_paths');
		$this->filenum 	= getheader('X-Weboffice-File-Id');
		if(isempt($this->filenum))$this->filenum = $this->get('_w_filenum');
		$this->frs		= m('file')->getone("`filenum`='$this->filenum'");
		$this->optid 	= $this->get('_w_optid','0'); //当前操作人id
		$this->optrs	= m('optus')->getone('`agentid`='.$this->agentid.' and `uid`='.$this->optid.'');
		//$this->rock->debugs($_SERVER, str_replace('/','_', $this->apipath));
		
		if(contain($this->apipath,'file/info'))return $this->fileinfo();
		if(contain($this->apipath,'file/save'))return $this->filesave();
		if(contain($this->apipath,'file/history'))return $this->filehistory();
		
		return 'sorry not mess';
	}
	
	
	//获取文件元数据
	private function fileinfo()
	{
		$filepath 		= $this->frs['filepath'];
		$filepathout	= $this->frs['filepathout'];
		$url 			= $this->rock->getouturl();
		$filename 		= $this->frs['filename'];
		
		if(!isempt($filepathout))$filepath=$filepathout;
		
		$file['id'] 	= $this->filenum;
		$file['name'] 	= $filename;
		$file['version']= 1;;
		$file['size'] 	= (int)$this->frs['filesize'];
		$file['creator']= $this->frs['optid'];
		$file['create_time'] = strtotime($this->frs['adddt']);
		$file['modify_time'] = time();
		$file['modifier'] 	 = $this->optid;
		
		$download_url = $filepath;
		if(substr($filepath,0,4)!='http')$download_url = ''.$url.''.$filepath.'';
		$file['download_url']= $download_url;
		
		$permission	= $this->get('_w_permission','read');
		
		$file['user_acl'] = array(
			'rename' 	=> 0,
			'history' 	=> 0,
			'export' 	=> 1,
			'copy' 		=> 1,
			'print' 	=> 1
		);
		$file['watermark'] = array(
			'type' => 0,
			'value' => '',
			'fillstyle' => 'rgba( 192, 192, 192, 0.6 )',
			'font' => 'bold 16px Serif',
			'rotate' => -0.7853982,
			'horizontal' => 50,
			'vertical' => 100,
		);
		//预览时加水印
		if($permission=='read'){
			$file['watermark']['type']=0; //0不加水印，1水印
			$file['watermark']['value']=arrvalue($this->optrs,'name');
		}
		
		$user = array(
			'id' 	=> $this->optid,
			'name' 	=> arrvalue($this->optrs,'name'),
			'permission' => $permission,
			'avatar_url' => $this->getface(arrvalue($this->optrs,'face')),
		);
		//$this->rock->debugs($file, 'fileinof');
		return array(
			'file'=>$file,
			'user'=>$user,
		);
	}
	
	private function getface($face)
	{
		$url 			= getconfig('outurl', URL);
		if(isempt($face))$face = 'images/noface.png';
		if(substr($face,0,4)!='http')$face = ''.$url.''.$face.'';
		return $face;
	}
	
	/**
	*	编辑完成保存
	*/
	public function filesave()
	{
		$filepath 		= $this->frs['filepath'];
		$filename 		= $this->frs['filename'];
		$fileext 		= $this->frs['fileext'];
		$filena 		= 'wps'.$this->frs['id'].'_'.date('dHis').'_'.rand(1000,9999).'';
		$fileurl		= $this->get('fileurl');

		if(isempt($fileurl)){
			$upobj 		= c('upfile');
			$upobj->initupfile('*', ''.UPDIR.'/'.date('Y-m').'', 50);
			$barr		= $upobj->up('file', $filena);
			if(is_string($barr)){
				$this->rock->debugs($barr, 'saveerr');
				return returnerror($barr);
			}
			$filepath	= $barr['allfilename'];
		}else{
			$filepath	= ''.UPDIR.'/'.date('Y-m').'/'.$filena.'.'.$fileext.'';
			$this->rock->createtxt($filepath, file_get_contents($this->jm->base64decode($fileurl)));
		}
		
		$banb 		= 1;
		m('file')->editcall($this->filenum, $filepath, $fileext);

		$file = array(
			'id' 		=> $this->filenum,
			'name' 		=> $filename,
			'version' 	=> $banb+1,
			'size' 		=> filesize($filepath),
			'download_url' => ''.getconfig('outurl', URL).''.$filepath.''
		);
		return array(
			'file'=>$file
		);
	}
	
	
	/**
	*	获取历史版本
	*/
	public function filehistory()
	{
		$data = json_decode($this->getpostdata(), true);
		
		$mid  = $this->frs['id']; 
		//$rows = m('files')->getall('`mid`='.$mid.'','*','id desc');
		$histories 		= array();
		$url 			= getconfig('outurl', URL);
		$create_time	= strtotime($this->frs['adddt']);
		//$usdb			= m('usera');
		//$crtrs 			= $usdb->getone($this->frs['optid']);
		if(1==2)foreach($rows as $k=>$rs){
			$edrs 			= $usdb->getone($rs['optid']);
			$histories[] = array(
				'id' 	=> $data['id'],
				'name' 	=> $this->frs['filename'],
				'version' => 2,
				'size' 	=> (int)$rs['filesize'],
				'download_url' => ''.$url.''.$rs['filepath'].'',
				'create_time' => $create_time,
				'modify_time' => strtotime($rs['optdt']),
				'creator' => array(
					'id' => $this->frs['optid'],
					'name' => $this->frs['optname'],
					'avatar_url' => $this->getface(arrvalue($crtrs,'face')),
				),
				'modifier' => array(
					'id' => $rs['optid'],
					'name' => $rs['optname'],
					'avatar_url' => $this->getface(arrvalue($edrs,'face')),
				)
			);
		}
		$histories = array(
			'histories' => $histories
		);
		//$this->rock->debugs($histories, 'history');
		return $histories;
	}
}